/*
 * @Descripttion: 
 * @version: 
 * @Author: lily
 * @Date: 2021-04-28 10:11:31
 * @LastEditors: lily
 * @LastEditTime: 2021-04-28 10:57:23
 */
/*
 * @lc app=leetcode.cn id=257 lang=javascript
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string[]}
 */
var binaryTreePaths = function (root) {
    let res = []

    var dfs = (node, curr) => {
        if (node) {
            curr.push(node.val)
            if (!node.left && !node.right) {
                res.push(curr.join('->'))
            }
            dfs(node.left, curr)
            dfs(node.right, curr)
            curr.pop()
        }
    }
    dfs(root, [])
    return res
};
// @lc code=end


function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val)
    this.left = (left === undefined ? null : left)
    this.right = (right === undefined ? null : right)
}

let r = new TreeNode(1, new TreeNode(2, new TreeNode(5), new TreeNode(6)), new TreeNode(3))
console.log(binaryTreePaths(r));